package com.example.model;

import java.util.List;
import java.util.ArrayList;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.controller.*;
import com.example.model.DatabaseAccess;
import com.example.model.DatabaseAccess.ExecutionType;

public class UserCRUD implements iCRUD<User, Integer, Integer> {
	
	@Override
	public void create(User instance) {
		HttpPost httpPost = new HttpPost("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectUserTask/helpConnectUser");
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("userID", Integer.toString(instance.getUserID())));
		nameValuePairs.add(new BasicNameValuePair("firstName", instance.getFirstName()));
		nameValuePairs.add(new BasicNameValuePair("lastName", instance.getLastName()));
		nameValuePairs.add(new BasicNameValuePair("email", instance.getEmail()));
		nameValuePairs.add(new BasicNameValuePair("phone", instance.getPhone()));
		nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(instance.getLatitude())));
		nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(instance.getLongitude())));
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch(UnsupportedEncodingException e) { 
			e.printStackTrace();
		}
		
		DataAccessBundle dab = new DataAccessBundle(httpPost, ExecutionType.CREATE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}

	@Override
	public User read(Integer ID) {
		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectUserTask/" + Integer.toString(ID));
		
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
		User user = null;
		if(result != null)
		{
			try {
				JSONObject record = new JSONObject(result);
				//System.out.println(record.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(record != null){
					user = new User(new UserCRUD());
					
					user.setUserID(record.getInt("UserID"));
					user.setFirstName((record.getString("FirstName") == "null" || record.getString("FirstName") == "") ? "": record.getString("FirstName"));
					user.setLastName((record.getString("LastName") == "null" || record.getString("LastName") == "") ? "": record.getString("LastName"));
					user.setEmail((record.getString("Email") == "null" || record.getString("Email") == "") ? "": record.getString("Email"));
					user.setPhone((record.getString("Phone") == "null" || record.getString("Phone") == "") ? "": record.getString("Phone"));
					user.setLatitude(record.getDouble("Latitude"));
					user.setLongitude(record.getDouble("Longitude"));
					user.setTimeStamp((record.getString("TimeStamp") == "null" || record.getString("TimeStamp") == "") ? "": record.getString("TimeStamp"));
				}
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
		}
		
		return user;
	}

	@Override
	public List<User> readAll() {
		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectUserTask");
		
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
		
		List<User> userList = new ArrayList<User>();
		
		if(result != null)
		{
			//System.out.println(result);
			try {
				JSONArray jSonArray = new JSONArray(result);
				//System.out.println(jSonArray.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(jSonArray.length() > 0){
					for(int i = 0; i < jSonArray.length(); i++){
						User user = new User(new UserCRUD());
						JSONObject record = jSonArray.getJSONObject(i);
						
						user.setUserID(record.getInt("UserID"));
						user.setFirstName((record.getString("FirstName") == null || record.getString("FirstName") == "") ? "": record.getString("FirstName"));
						user.setLastName((record.getString("LastName") == null || record.getString("LastName") == "") ? "": record.getString("LastName"));
						user.setEmail((record.getString("Email") == "null" || record.getString("Email") == "") ? "": record.getString("Email"));
						user.setPhone((record.getString("Phone") == "null" || record.getString("Phone") == "") ? "": record.getString("Phone"));
						user.setLatitude(record.getDouble("Latitude"));
						user.setLongitude(record.getDouble("Longitude"));
						user.setTimeStamp((record.getString("TimeStamp") == "null" || record.getString("TimeStamp") == "") ? "": record.getString("TimeStamp"));
						
						userList.add(user);
						
					}
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return userList;
	}

	@Override
	public void update(User instance) {
		
		HttpPost httpPut = new HttpPost("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectUserTask/" + Integer.toString(instance.getUserID()));
		httpPut.addHeader("X-HTTP-Method-Override", "PUT");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("firstName", instance.getFirstName()));
		nameValuePairs.add(new BasicNameValuePair("lastName", instance.getLastName()));
		nameValuePairs.add(new BasicNameValuePair("email", instance.getEmail()));
		nameValuePairs.add(new BasicNameValuePair("phone", instance.getPhone()));
		nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(instance.getLatitude())));
		nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(instance.getLongitude())));
		try {
			httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch (UnsupportedEncodingException e) { 
			e.printStackTrace();
		}
		
		DataAccessBundle dab = new DataAccessBundle(httpPut, ExecutionType.UPDATE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}

	@Override
	public void delete(Integer ID) {
		HttpDelete httpDelete = new HttpDelete("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectUserTask/" + Integer.toString(ID));

		DataAccessBundle dab = new DataAccessBundle(httpDelete, ExecutionType.DELETE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}


}
